package fl.ass.service.api;

import java.util.List;

import cn.cmm.base.framework.utils.BaseLogUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.comm.facade.framework.dto.SysDictionaryDTO;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.StringUtils;
import fl.ass.facade.api.IAssDealSellApi;
import fl.ass.facade.condition.AssDealSellLossQueryCondition;
import fl.ass.facade.condition.AssDealSellQueryCondition;
import fl.ass.facade.constant.InterfaceConstants;
import fl.ass.facade.dto.AssDealSellDTO;
import fl.ass.facade.dto.AssDealSellLossDTO;
import fl.ass.facade.dto.MessageDTO;
import fl.ass.service.service.IAssDealSellLossService;
import fl.ass.service.service.IAssDealSellService;

/**
 * 处置变卖API
 * @author zy
 *
 */
@Component
public class AssDealSellApi implements IAssDealSellApi {
	
	private static final Logger logger = LoggerFactory.getLogger(AssDealSellApi.class);
	
	@Autowired
	private IAssDealSellService assDealSellService;
	@Autowired
	private IAssDealSellLossService assDealSellLossService;
	
	@Autowired
	private ISysDictionaryApiService sysDictionaryApiService;

	/* (non-Javadoc)
	 * @see fl.ass.facade.api.IAssDealSellApi#saveAssDealSell(fl.ass.facade.dto.AssDealSellDTO)
	 */
	@Override
	public MessageDTO<String> saveAssDealSell(AssDealSellDTO dto) {
		MessageDTO<String> response = new MessageDTO<String>();
		if(null == dto){
			response.setMessage("parameter：dto is can not be null");
		}
		try {
			Long id;
            if (StringUtils.isEmpty(dto.getId())) {
                id = assDealSellService.add(dto);
            } else {
                id = assDealSellService.addWithId(dto);
            }
			response.setSuccess(true);
			response.setId(id);
		} catch (Exception e) {
            BaseLogUtils.newLogger("异常!" + e.getMessage()).setThrowable(e).info();
			response.setMessage(e.getMessage());
		}
		return response;
	}

	/* (non-Javadoc)
	 * @see fl.ass.facade.api.IAssDealSellApi#updateAssDealSell(fl.ass.facade.dto.AssDealSellDTO)
	 */
	@Override
	public MessageDTO<String> updateAssDealSell(AssDealSellDTO dto) {
		MessageDTO<String> response = new MessageDTO<String>();
		if(null == dto){
			response.setMessage("parameter：dto is can not be null");
		}
		try {
			boolean flag = assDealSellService.modify(dto);
			response.setSuccess(flag);
		} catch (Exception e) {
            BaseLogUtils.newLogger("异常!" + e.getMessage()).setThrowable(e).info();
			response.setMessage(e.getMessage());
		}
		return response;
	}

	/* (non-Javadoc)
	 * @see fl.ass.facade.api.IAssDealSellApi#findAssDealSellById(java.lang.Long)
	 */
	@Override
	public MessageDTO<AssDealSellDTO> findAssDealSellById(Long id) {
		MessageDTO<AssDealSellDTO> response = new MessageDTO<AssDealSellDTO>();
		if(null == id){
			response.setMessage("parameter：id is can not be null");
		}
		try {
			AssDealSellDTO dto = assDealSellService.selectById(id);
			SysDictionaryDTO dictionary = null;
			if(null != dto){
				//处理拖回标记
				if(StringUtils.isNotBlank(dto.getBackedFlagCd())){
					dictionary = sysDictionaryApiService.getCode(InterfaceConstants.SYS_CODE, InterfaceConstants.ORG_CODE, InterfaceConstants.BACK_FLAG_CD, dto.getBackedFlagCd());
					if(null != dictionary){
						dto.setBackedFlagCd(dictionary.getCodeNm());
					}
				}
				response.setData(dto);
			}
			response.setSuccess(true);
		} catch (Exception e) {
            BaseLogUtils.newLogger("异常!" + e.getMessage()).setThrowable(e).info();
			response.setMessage(e.getMessage());
		}
		return response;
	}

	@Override
	public MessageDTO<AssDealSellDTO> searchList(
			AssDealSellQueryCondition condition) throws BaseException {
		MessageDTO<AssDealSellDTO> response = new MessageDTO<AssDealSellDTO>();
		if(null == condition.getDetailId() && null == condition.getMasterId()){
			response.setMessage("parameter：ids is can not be null");
		}
		try {
			List<AssDealSellDTO> dtos = assDealSellService.searchList(condition);
			if(null != dtos){
				response.setDatas(dtos);
			}
			response.setSuccess(true);
		} catch (Exception e) {
            BaseLogUtils.newLogger("异常!" + e.getMessage()).setThrowable(e).info();
			response.setMessage(e.getMessage());
		}
		return response;
		}
	 /**
     * 分页查询所有变卖亏损信息
     * @param page
     * @return
     */
	@Override
	public Pager AssDealSellLossByPage(Pager page) throws BaseException{
		Pager assDealSellLossByPage = this.assDealSellLossService.AssDealSellLossByPage(page);
		return assDealSellLossByPage;
	}

	@Override
	public MessageDTO<AssDealSellLossDTO> searchLossList(
			Long  SellId) throws BaseException {
		MessageDTO<AssDealSellLossDTO> response = new MessageDTO<AssDealSellLossDTO>();
		if(null  == SellId){
			response.setMessage("parameter：ids is can not be null");
		}
		try {
			List<AssDealSellLossDTO> dtos = assDealSellLossService.searchLossList(SellId);
			if(null != dtos){
				response.setDatas(dtos);
			}
			response.setSuccess(true);
		} catch (Exception e) {
            BaseLogUtils.newLogger("异常!" + e.getMessage()).setThrowable(e).info();
			response.setMessage(e.getMessage());
		}
		return response;
	}
	/*
	 * 新增亏损信息
	 */
	@Override
	public MessageDTO<String> addassDealSellLoss(AssDealSellLossDTO dto)
			throws BaseException {
		MessageDTO<String> response = new MessageDTO<String>();
		if(null == dto){
			response.setMessage("parameter：dto is can not be null");
		}
		try {
			Long id = assDealSellLossService.addassDealSellLoss(dto);
			response.setId(id);
			response.setSuccess(true);
		} catch (Exception e) {
            BaseLogUtils.newLogger("异常!" + e.getMessage()).setThrowable(e).info();
			response.setMessage(e.getMessage());
		}
		return response;
	}
	/*
	 * 修改亏损信息
	 */
	@Override
	public MessageDTO<String> modifyassDealSellLoss(AssDealSellLossDTO dto)
			throws BaseException {
		MessageDTO<String> response = new MessageDTO<String>();
		if(null == dto){
			response.setMessage("parameter：dto is can not be null");
		}
		try {
			Long id = assDealSellLossService.modifyassDealSellLoss(dto);
			response.setId(id);
			response.setSuccess(true);
		} catch (Exception e) {
            BaseLogUtils.newLogger("异常!" + e.getMessage()).setThrowable(e).info();
			response.setMessage(e.getMessage());
		}
		return response;
	}

}
